//
//  SWFormInputCell.m
//  SWFormDemo
//
//  Created by zijin on 2018/5/28.
//  Copyright © 2018年 selwyn. All rights reserved.
//

#import "SWFormInputCell.h"
#import "SWFormItem.h"
#import "SWFormCompat.h"
#import "UITextField+TextLimit.h"
#import "NSString+SWForm.h"

@interface SWFormInputCell()<UITextFieldDelegate>
@end

@implementation SWFormInputCell

- (void)setItem:(SWFormItem *)item {
    _item = item;
    self.titleLabel.attributedText = item.attributedTitle;
    if (item.unit) {
        self.unitLabel.text = item.unit;
    }
    self.textField.text = item.info;
    self.textField.attributedPlaceholder = item.attributedPlaceholder;
    self.textField.enabled = item.editable;
    self.textField.keyboardType = item.keyboardType;
    self.accessoryType = UITableViewCellAccessoryNone;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (BOOL)textFieldDidChange:(NSNotification *)notification {
    UITextField *textField = notification.object;
    if (self.item.maxInputLength > 0) {
        // 限制输入字数
        [self.textField textLimitWithMaxLength:self.item.maxInputLength];
    }
    if (self.inputCompletion && textField == self.textField && textField.markedTextRange == nil) {
        self.inputCompletion(self.textField.text);
    }
    // 防止输入时表单因刷新动画抖动
    [UIView performWithoutAnimation:^{
        [self.expandableTableView beginUpdates];
        [self.expandableTableView endUpdates];
    }];
    return YES;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.titleLabel.frame = CGRectMake(SW_EdgeMargin, SW_EdgeMargin, SW_TitleWidth, SW_TitleHeight);
    
//    CGFloat margin = 8.0;
//    WeakSelf
//    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(weakSelf.contentView).offset(2 * margin);
//        make.top.mas_equalTo(weakSelf.contentView).offset(margin);
//        make.bottom.mas_equalTo(weakSelf.contentView).offset(-margin);
//    }];
    
    CGFloat newHeight = [SWFormInputCell heightWithItem:self.item];
    if (self.item.unit.length > 0) {
        self.textField.frame = CGRectMake(SW_TitleWidth + 2*SW_EdgeMargin, SW_EdgeMargin, SW_SCRREN_WIDTH - (SW_TitleWidth + 8*SW_EdgeMargin), newHeight - 2*SW_EdgeMargin);
        self.unitLabel.frame = CGRectMake(SW_SCRREN_WIDTH - 5*SW_EdgeMargin, SW_EdgeMargin, 4*SW_EdgeMargin, SW_TitleHeight);
    } else {
        self.textField.frame = CGRectMake(SW_TitleWidth + 2*SW_EdgeMargin, SW_EdgeMargin, SW_SCRREN_WIDTH - (SW_TitleWidth + 3*SW_EdgeMargin), newHeight - 2*SW_EdgeMargin);
    }
}



- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.textField.text = self.item.info;
}



- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.textField.text = self.item.info;
    if ([self.delegete respondsToSelector:@selector(contentDidChangeTextWithCell:text:)]) {
        [self.delegete contentDidChangeTextWithCell:self text:textField.text];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([self.delegete respondsToSelector:@selector(contentShouldChangeTextWithCell:text:)]) {
       return [self.delegete contentShouldChangeTextWithCell:self text:[NSString stringWithFormat:@"%@%@",textField.text,string]];
    }
    return YES;
}

+ (CGFloat)heightWithItem:(SWFormItem *)item {
    return item.defaultHeight;
//    CGFloat infoHeight = [item.info sizeWithFontSize:SW_InfoFont maxSize:CGSizeMake(SW_SCRREN_WIDTH - (SW_TitleWidth + 7*SW_EdgeMargin), MAXFLOAT)].height;
//    return MAX(item.defaultHeight, infoHeight + 2*SW_EdgeMargin);
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (UILabel *)unitLabel {
    if (!_unitLabel) {
        _unitLabel = [[UILabel alloc] init];
        _unitLabel.font = [UIFont systemFontOfSize:13];
        _unitLabel.adjustsFontSizeToFitWidth = YES;
        [self.contentView addSubview:_unitLabel];
    }
    return _unitLabel;
}

@end


@implementation UITableView (SWFormInputCell)

- (SWFormInputCell *)inputCellWithId:(NSString *)cellId {
    SWFormInputCell *cell = [self dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[SWFormInputCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.expandableTableView = self;
    }
    return cell;
}

@end
